package com.Test;

import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageEncoder;

import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.Random;

/**
 * Created by Administrator on 2016/3/26.
 */
@WebServlet(name = "indexServlet")
public class indexServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.log("执行doPost()...方法!!!");
        this.doGet(request,response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
      //response.setCharacterEncoding("UTF-8"); //设置编码方式
      //  request.setCharacterEncoding("UTF-8");
      //  response.setContentType("text/html"); //设置文档类型
      //  this.log("执行doGet()方法......");
      //  /**
      //   *getServletContext()取得的是 <context-param>配置的参数
      //   *getServletConfig()取得的是 <servlet> <init-param>配置的参数
      //   */
      //  String message = getServletConfig().getInitParameter("message");
      //  String encoding = getServletConfig().getInitParameter("encoding");
      //  String message1 = getServletContext().getInitParameter("message1");
      //  String encoding1 = getServletContext().getInitParameter("encoding1");
      //  PrintWriter out = response.getWriter();
      //  out.println("</br>getServletConfig().getInitParameter()");
      //  out.println(message);
      //  out.println(encoding);
      //  out.println("</br>getServletContext().getInitParameter()");
      //  out.println(message1);
      //  out.println(encoding1);
      //  out.flush();
      //  out.close();
        response.setContentType("image/jpeg"); //设置输出类型
        String randomString = getRandomString();
        request.getSession(true).setAttribute("randomString",randomString);//放入session中
        int width = 100,height = 30;
        Color color = getRandomColor();
        Color reverseColor = getReverseColor(color);
        BufferedImage bi = new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB); //创建一个彩色图片
        Graphics2D g = bi.createGraphics(); //获取绘图对象
        g.setFont(new Font(Font.SANS_SERIF,Font.BOLD,16));
        g.setColor(color);
        g.fillRect(0,0,width,height);
        g.setColor(reverseColor);
        g.drawString(randomString,18,20);//绘制随机字符
        for(int i = 0,n = random.nextInt(100); i<n; i++){
            g.drawRect(random.nextInt(width),random.nextInt(height),1,1);//设置噪音点
        }
        ServletOutputStream out = response.getOutputStream(); //转成JPEG格式
        JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out); //编码器
        encoder.encode(bi);
        out.flush(); //输出到客户端
    }

    /**
     * 返回该servlet生成文档的更新时间。对get方式访问有效
     * @param request
     * @return
     */
    @Override
    public long getLastModified(HttpServletRequest request){
        this.log("执行getLastModified()方法.....");
        return -1;
    }

    public static final char[] CHARS = {'1','2','3','4','5','6','7','8','9','A','B','C','D','E',
            'F','G','H','I','J','K','L','M','N','P','Q','R','S','T','U','V','W','X','Y','Z'};
    public static Random random = new Random();

    /**
     * 获取6位随机数
     * @return
     */
    public static String getRandomString(){
        StringBuffer buffer = new StringBuffer();
        for(int i=0; i<6; i++){
            buffer.append(CHARS[random.nextInt(CHARS.length)]);
        }
        return buffer.toString();
    }

    /**
     * 获取随机颜色
     * @return
     */
    public static Color getRandomColor(){
        return new Color(random.nextInt(255),random.nextInt(255),random.nextInt(255));
    }

    /**
     * 获取颜色的反色
     * @param c
     * @return
     */
    public static Color getReverseColor(Color c){
        return new Color(255-c.getRed(),255-c.getGreen(),255-c.getBlue());
    }
}
